package com.quanyan.user.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.constants.Constants;
import com.quanyan.user.mapper.TbPushArticleMapper;
import com.quanyan.user.mapper.TbPushReadAndPraiseMapper;
import com.quanyan.user.mapper.TbUserReadAndPraiseMapper;
import com.quanyan.user.model.db.*;
import com.quanyan.user.model.vo.response.RespReadAndPraise;
import com.quanyan.user.service.UserPushService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhang on 16/12/5.
 */
@Service
public class UserPushServiceImpl implements UserPushService {

    private static final Logger logger = LoggerFactory.getLogger(UserPushServiceImpl.class);

    @Autowired
    private TbPushReadAndPraiseMapper tbPushReadAndPraiseMapper;

    @Autowired
    private TbUserReadAndPraiseMapper tbUserReadAndPraiseMapper;

    @Autowired
    private TbPushArticleMapper tbPushArticleMapper;

    private static final byte IS_ARTICLE_PRAISED = 1;
    private static final byte IS_ARTICLE_NOT_PRAISED = 0;


    @Override
    public APIResponse<RespReadAndPraise> readAndPraise(Integer uid, Integer type, Integer articleId) {

        if (uid < 0 || (type != 1 && type != 2) || articleId < 0) {
            return APIResponse.returnFail("参数错误");
        }

        TbPushArticle tbPushArticle = tbPushArticleMapper.selectByPrimaryKey(articleId);
        if (tbPushArticle == null) {
            logger.debug("invalid article Id : " + articleId);
            return APIResponse.returnFail("该文章不存在");
        }

        TbPushReadAndPraiseExample example = new TbPushReadAndPraiseExample();
        TbPushReadAndPraiseExample.Criteria criteria = example.createCriteria();
        criteria.andArticleIdEqualTo(articleId);
        List<TbPushReadAndPraise> tbArticleList = tbPushReadAndPraiseMapper.selectByExample(example);

        TbPushReadAndPraise tbPushReadAndPraise = new TbPushReadAndPraise();
        tbPushReadAndPraise.setArticleId(articleId);
        tbPushReadAndPraise.setPraiseNum(0);
        tbPushReadAndPraise.setReadNum(0);
        //该文章刚发布,未被阅读或点赞
        if (tbArticleList == null || tbArticleList.size() == 0) {
            if (type == 1) {
                tbPushReadAndPraise.setReadNum(1);
            } else {
                tbPushReadAndPraise.setPraiseNum(1);
            }
            tbPushReadAndPraiseMapper.insertSelective(tbPushReadAndPraise);
        } else {
            tbPushReadAndPraise = tbArticleList.get(0);
            if (tbPushReadAndPraise.getIsDelete() != Constants.IS_DELETE_FALSE) {
                return APIResponse.returnFail("该文章不能评论或点赞");
            }

            if (type == 1) {
                tbPushReadAndPraise.setReadNum(tbPushReadAndPraise.getReadNum() + 1);
                tbPushReadAndPraiseMapper.updateByPrimaryKeySelective(tbPushReadAndPraise);
            }
        }


        RespReadAndPraise respReadAndPraise = new RespReadAndPraise();
        respReadAndPraise.setPaid(tbPushReadAndPraise.getArticleId());
        respReadAndPraise.setReadNum(tbPushReadAndPraise.getReadNum());


        TbUserReadAndPraiseExample exampleUser = new TbUserReadAndPraiseExample();
        TbUserReadAndPraiseExample.Criteria criteriaUser = exampleUser.createCriteria();
        criteriaUser.andUidEqualTo(uid);
        criteriaUser.andArticleIdEqualTo(articleId);

        List<TbUserReadAndPraise> tbUserList = tbUserReadAndPraiseMapper.selectByExample(exampleUser);

        TbUserReadAndPraise tbUserReadAndPraise = new TbUserReadAndPraise();
        byte isPraise = IS_ARTICLE_NOT_PRAISED;
        //用户之前没点赞过
        if (tbUserList == null || tbUserList.size() == 0) {
            tbUserReadAndPraise.setArticleId(articleId);
            tbUserReadAndPraise.setUid(uid);
            tbUserReadAndPraise.setIsDelete(Constants.IS_DELETE_FALSE);

            isPraise = type == 1 ? IS_ARTICLE_NOT_PRAISED : IS_ARTICLE_PRAISED;

            if(type == 2){
                tbPushReadAndPraise.setPraiseNum(tbPushReadAndPraise.getPraiseNum() + 1);
                tbPushReadAndPraiseMapper.updateByPrimaryKeySelective(tbPushReadAndPraise);
            }

            tbUserReadAndPraise.setIsPraise(isPraise);
            respReadAndPraise.setIsPraise(isPraise);

            tbUserReadAndPraiseMapper.insertSelective(tbUserReadAndPraise);
        } else {
            tbUserReadAndPraise = tbUserList.get(0);

            //如果是点赞且该用户之前未点过赞
            isPraise = tbUserReadAndPraise.getIsPraise();

            if(type == 2){
                if(tbUserReadAndPraise.getIsPraise() == IS_ARTICLE_NOT_PRAISED){
                    //未点赞, 则点赞
                    isPraise = IS_ARTICLE_PRAISED;
                    tbUserReadAndPraise.setIsPraise(isPraise);
                    tbPushReadAndPraise.setPraiseNum(tbPushReadAndPraise.getPraiseNum() + 1);
                    tbPushReadAndPraiseMapper.updateByPrimaryKeySelective(tbPushReadAndPraise);
                }else if(tbUserReadAndPraise.getIsPraise() == IS_ARTICLE_PRAISED){
                    //已点赞, 则取消点赞
                    isPraise = IS_ARTICLE_NOT_PRAISED;
                    tbUserReadAndPraise.setIsPraise(isPraise);
                    tbPushReadAndPraise.setPraiseNum(tbPushReadAndPraise.getPraiseNum() - 1);
                    tbPushReadAndPraiseMapper.updateByPrimaryKeySelective(tbPushReadAndPraise);
                }
            }

            respReadAndPraise.setIsPraise(isPraise);
            tbUserReadAndPraiseMapper.updateByPrimaryKeySelective(tbUserReadAndPraise);
        }

        respReadAndPraise.setPraiseNum(tbPushReadAndPraise.getPraiseNum());

        return APIResponse.returnSuccess(respReadAndPraise);
    }


}
